package com.example.musicplayer.util

import android.content.Context
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper

class UserProfileDatabaseHelper(context: Context) :
    SQLiteOpenHelper(context, DATABASE_NAME, null, DATABASE_VERSION) {

    companion object {
        internal const val DATABASE_NAME = "user_profile.db"
        const val DATABASE_VERSION = 3 // 增加版本号

        const val TABLE_USER_PROFILE = "user_profile"
        const val COLUMN_ID = "_id"
        const val COLUMN_USERNAME = "username"
        const val COLUMN_BIO = "bio"
        const val COLUMN_PHONE = "phone"
        const val COLUMN_GENDER = "gender"
        const val COLUMN_BIRTHDAY = "birthday"
        const val COLUMN_LOCATION = "location"
        const val COLUMN_SCHOOL = "school"
        const val COLUMN_IMAGE = "image" // 更改字段名
        const val COLUMN_PASSWORD = "password"

        private const val CREATE_TABLE_USER_PROFILE = """
            CREATE TABLE $TABLE_USER_PROFILE (
                $COLUMN_ID INTEGER PRIMARY KEY AUTOINCREMENT,
                $COLUMN_USERNAME TEXT,
                $COLUMN_BIO TEXT,
                $COLUMN_PHONE TEXT,
                $COLUMN_GENDER TEXT,
                $COLUMN_BIRTHDAY TEXT,
                $COLUMN_LOCATION TEXT,
                $COLUMN_SCHOOL TEXT,
                $COLUMN_IMAGE BLOB,
                $COLUMN_PASSWORD TEXT
            )
        """
    }

    override fun onCreate(db: SQLiteDatabase) {
        db.execSQL(CREATE_TABLE_USER_PROFILE)
    }

    override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
        if (oldVersion < 3) {
            // 删除旧表
            db.execSQL("DROP TABLE IF EXISTS $TABLE_USER_PROFILE")
            // 创建新表
            onCreate(db)
        }
    }
}